package com.wwt.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wwt.server.pojo.MenuRole;
import com.wwt.server.pojo.Role;
import com.wwt.server.service.IMenuRoleService;
import com.wwt.server.service.IMenuService;
import com.wwt.server.service.IRoleService;
import com.wwt.server.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/basic/permiss")
@Api(tags = "角色管理控制器")
public class PermissionController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;

    @GetMapping("/")
    @ApiOperation(value = "获取所有角色")
    public Result getAllRoles() {
        List<Role> list = roleService.list();
        return Result.success("获取成功！", list);
    }

    @PostMapping("/role/")
    @ApiOperation(value = "添加角色")
    public Result addRole(@RequestBody Role role) {
        //先判断角色名是否符合SpringSecurity的规则
        if (!role.getName().startsWith("ROLE_"))
            role.setName("ROLE_" + role.getName());
        //判断是否重复
        QueryWrapper<Role> wrapper = new QueryWrapper<Role>()
                .eq("name", role.getName());
        Role existRole = roleService.getOne(wrapper);
        boolean result = false;
        if (existRole == null) {
            result = roleService.save(role);
            if (result)
                return Result.success("添加成功！");
            return Result.fail("添加失败！");
        }
        return Result.fail("失败，该角色已存在！");
    }

    @DeleteMapping("/role/{rid}")
    @ApiOperation(value = "删除角色")
    public Result deleteRole(@PathVariable("rid") Integer rid) {
        boolean result = roleService.removeById(rid);
        if (result)
            return Result.success("删除成功！");
        return Result.fail("删除失败，请重试！");
    }

    @DeleteMapping("/role/")
    @ApiOperation(value = "按角色名删除角色")
    public Result deleteRoleByRoleName(@RequestBody String roleName) {
        if (!roleName.startsWith("ROLE_"))
            roleName = "ROLE_" + roleName;
        boolean result = roleService.deleteByRoleName(roleName);
        if (result)
            return Result.success("删除成功！");
        return Result.fail("删除失败，请重试！");
    }

    @ApiOperation(value = "获取所有菜单")
    @GetMapping("/menus")
    public Result getAllMenus() {
         return menuService.getAllMenus();
    }

    @ApiOperation(value = "根据角色ID查询菜单ID")
    @GetMapping("/mid/{rid}")
    public List<Integer> getAMenuIdByRoleId(@PathVariable("rid") Integer rid) {
        QueryWrapper<MenuRole> wrapper = new QueryWrapper<MenuRole>().eq("rid",rid);
        List<MenuRole> list = menuRoleService.list(wrapper);
        List<Integer> integers = list.stream().map(MenuRole::getMid).collect(Collectors.toList());
        return integers;
    }

    @ApiOperation(value = "更新角色拥有的菜单")
    @PutMapping("/")
    public Result updateMenuRole(Integer rid,Integer[] mids){
       return menuRoleService.updateMenuRole(rid,mids);
    }
}
